﻿using MahApps.Metro.Controls;
using Mengsk.ProxyDelivery.App.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App
{
    /// <summary>
    /// MainWindow3.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow3 : Window
    {
        private List<TreeViewItem> openedITreeViewItems = new List<TreeViewItem>();

        public MainWindow3()
        {
            InitializeComponent();
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.tvMenus.Items.Clear();
            PluginGroup[] groups = IPluginManager.LoadAllUIPluginGroups();
            foreach (var group in groups)
            {
                TreeViewItem item = new TreeViewItem { Header = group.Title, FontFamily = new FontFamily("黑体"), FontSize = 16 };
                foreach (var plugin in group.Plugins)
                {
                    TreeViewItem itemSub = new TreeViewItem { Header = plugin.Title, FontFamily = new FontFamily("微软雅黑"), FontSize = 14, DataContext = plugin };
                    item.Items.Add(itemSub);
                }
                this.tvMenus.Items.Add(item);
            }
        }

        private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("是否退出?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
            {
                e.Cancel = true;
            }
        }

        private void tvMenus_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                TreeViewItem item = e.NewValue as TreeViewItem;
                if (item == null || item.DataContext == null)
                {
                    return;
                }
                TreeViewItem opened = this.openedITreeViewItems.FirstOrDefault(obj => obj == item);
                if (opened != null)
                {
                    int index = this.openedITreeViewItems.IndexOf(opened);
                    this.tbHosts.SelectedIndex = index;
                    return;
                }
                IPlugin plugin = item.DataContext as IPlugin;
                //创建tab
                MetroTabItem tabItem = new MetroTabItem(this.tbHosts);
                tabItem.Header = plugin.Title;
                tabItem.Content = plugin.UIControl;
                tabItem.CloseButtonEnabled = true;
                tabItem.DataContext = item;
                tabItem.CloseTabCommand = new TabCloseHandler(tabItem, CloseTab);
                this.tbHosts.Items.Add(tabItem);
                this.openedITreeViewItems.Add(item);
                ControlsHelper.SetHeaderFontSize(tabItem, 16);
                this.tbHosts.SelectedIndex = this.openedITreeViewItems.IndexOf(item);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void CloseTab(MetroTabItem parameter)
        {
            TreeViewItem item = parameter.DataContext as TreeViewItem;
            this.openedITreeViewItems.Remove(item);
            this.tbHosts.Items.Remove(parameter);
        }
    }

    public class TabCloseHandler : ICommand
    {
        public Action<MetroTabItem> Action { get; set; }

        public MetroTabItem MetroTabItem { get; set; }

        public TabCloseHandler(MetroTabItem treeViewItem, Action<MetroTabItem> action)
        {
            this.Action = action;
            this.MetroTabItem = treeViewItem;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            this.Action(this.MetroTabItem);
        }
    }


}
